<?php
import('@.Action.PublicAction');
class PlanAction extends Action
{
    function _initialize() 
    {
       if(!isset($_SESSION['user_right'])) {
			$this->redirect('login','Public');
        }
        parent::_initialize();
    }
    	
	function index()
	{
		$_SESSION["PLAN_ID"]=null;
		//列表过滤器，生成查询Map对象
        $map = $this->_search();		
        if(method_exists($this,'_filter')) {
            $this->_filter($map);
        }
		$model        = D('Plan');
        if(!empty($model)) {
        	$this->_list($model,$map);
        }
		$this->display();
        return;
		
	}
    /**
     +----------------------------------------------------------
     * 根据表单生成查询条件
     * 进行列表过滤
     +----------------------------------------------------------
     * @access protected 
     +----------------------------------------------------------
     * @param string $name 数据对象名称 
     +----------------------------------------------------------
     * @return HashMap
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    protected function _search($name='') 
    {
        //生成查询条件
		if(empty($name)) {
			$name	=	$this->name;
		}
		$model	=	D($name);
		$map	=	array();
		foreach($model->getDbFields() as $key=>$val) {
            if(isset($_REQUEST[$val]) && $_REQUEST[$val]!='') {
                //原来的==查询
            	$map[$val]	=	$_REQUEST[$val];
            }
        }

        return $map;
    }	
    /**
     +----------------------------------------------------------
     * 根据表单生成查询条件
     * 进行列表过滤
     +----------------------------------------------------------
     * @access protected 
     +----------------------------------------------------------
     * @param Model $model 数据对象 
     * @param HashMap $map 过滤条件 
     * @param string $sortBy 排序 
     * @param boolean $asc 是否正序 
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    protected function _list($model,$map,$sortBy='',$asc=true) 
    {
        //排序字段 默认为主键名
        if(isset($_REQUEST['order'])) {
            $order = $_REQUEST['order'];
        }else {
            $order = !empty($sortBy)? $sortBy: $model->getPk();
        }
        //排序方式默认按照倒序排列
        //接受 sost参数 0 表示倒序 非0都 表示正序
        if(isset($_REQUEST['sort'])) {
            $sort = $_REQUEST['sort']?'asc':'desc';
        }else {
            $sort = $asc?'asc':'desc';
        }

        //dump($map);
		/*添加HashMap来修改成模糊查询 link %%*/
		import("ORG.Util.HashMap");
		$maptmp=new HashMap();
        /**/
		foreach ($map as $key=>$val){
			$maptmp->put($key,array('like',"%".$val."%"));

		}
		//取得满足条件的记录数
        $count      = $model->count($maptmp);
		if($count>0) {
			import("ORG.Util.Page");
			//创建分页对象
			if(!empty($_REQUEST['listRows'])) {
				$listRows  =  $_REQUEST['listRows'];
			}else {
				$listRows  =  '';
			}
			$p          = new Page($count,$listRows);
			//分页查询数据
			$voList     = $model->findAll($maptmp,'*',$order.' '.$sort,$p->firstRow.','.$p->listRows);
			for($i=0;$i<count($voList);$i++)
			{
				if($voList[$i]['PLAN_MODE']=="limit")
				{
					$voList[$i]['PLAN_MODE']="限制";
				}
				else
				{
					$voList[$i]['PLAN_MODE']="无限制";
				}
			}
			
			//$voList     = $model->findAll("xh like '%200555%'",'*',$order.' '.$sort,$p->firstRow.','.$p->listRows);
			//分页跳转的时候保证查询条件
			foreach($map as $key=>$val) {
				if(is_array($val)) {
					foreach ($val as $t){
						$p->parameter	.= $key.'[]='.urlencode($t)."&";
					}
				}else{
					$p->parameter   .=   "$key=".urlencode($val)."&";        
				}
			}
			//分页显示
			$page       = $p->show();
			//列表排序显示
			$sortImg    = $sort ;                                   //排序图标
			$sortAlt    = $sort == 'desc'?'升序排列':'倒序排列';    //排序提示
			$sort       = $sort == 'desc'? 1:0;                     //排序方式
			//模板赋值显示
			$this->assign('list',       $voList);
			$this->assign('sort',       $sort);
			$this->assign('order',      $order);
			$this->assign('sortImg',    $sortImg);
			$this->assign('sortType',   $sortAlt);
			$this->assign("page",       $page);
		}
        return ;
    }
	

    /**
     +----------------------------------------------------------
     * 默认删除操作
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    public function delete()
    {
        //删除指定记录
        $model        = D('Plan');
        if(!empty($model)) {
            $id         = $_REQUEST['id'];
            if(isset($id)) {
				//删除项目子选项
				$Dao=D('Plan_option');
				$Dao->delete("`PLAN_ID` in (".$id.")");				
				//删除项目配额
				$Dao=D('Plan_quota');
				$Dao->delete("`PLAN_ID` in (".$id.")");	
                if($model->delete($id)){
                    $this->success(L('_DELETE_SUCCESS_'));
                }else {
                    $this->error(L('_DELETE_FAIL_'));
                }        	
            }else {
                $this->error('非法操作');
            }        	
        }
    }

    /**
     +----------------------------------------------------------
     * 默认禁用操作
     * 
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    function forbid()
    {
    	print_r($_GET);
		$model	=	D('Plan');
        $condition = 'PLAN_ID IN ('.$_GET['xh'].')'; 
        if($model->setField('PLAN_STATUS','0',$condition)){
            $this->assign("message",'状态禁用成功！');
            $this->assign("jumpUrl",$this->getReturnUrl());
        }else {
            $this->assign('error',  '状态禁用失败！');
        }
        $this->forward();
    }

    /**
     +----------------------------------------------------------
     * 默认恢复操作
     * 
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     * @throws FcsException
     +----------------------------------------------------------
     */
    function resume()
    {
    	//恢复指定记录
		$model	=	D('Plan');
        $condition = 'PLAN_ID IN (\''.$_GET['id'].'\')';
        //echo $condition; 
        //exit();
        if($model->setField('PLAN_STATUS','1',$condition)){
            $this->assign("message",'状态恢复成功！');
            $this->assign("jumpUrl",$this->getReturnUrl());
        }else {
            $this->assign('error',  '状态恢复失败！');
        }
        $this->forward();
    }
	
	function add() 
	{ 
		// 添加内容的页面 
		$this->display(); 
	} 

	function insert() 
	{ 
		// 保存添加的数据
		$Dao = D("Plan"); 
		$vo = $Dao->create(); 
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		} 
 
		$rs = $Dao->add(); 
		if($rs){ 
			$this->assign("jumpUrl",__URL__); 
			$this->success("添加数据成功!"); 
		}else{ 
			$this->error("添加数据失败"); 
		} 
	}

	function edit() 
	{ 
		// 编辑页面 
		$Dao = D("Plan"); 
		$vo = $Dao->find("PLAN_ID=".$_GET["id"]); 
		if($vo === false) 
		{ 
			$this->assign("jumpUrl",__URL__); 
			$this->error("Non-existed record"); 
		} 
		 
		$this->assign("vo",$vo); 
		$this->display(); 
	} 

	function update() 
	{ 
		// 保存编辑过的数据 
		$Dao = D("Plan"); 
		$vo = $Dao->create("","edit");
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		} 
 
		$rs = $Dao->save(); 
		if($rs){ 
			$this->assign("jumpUrl",__URL__); 
			$this->success("保存修改成功!"); 
		}else{ 
			$this->error("保存修改失败"); 
		} 
	}
	
	function optionIndex(){
		//项目子名称主页面
		if(isset($_SESSION["PLAN_ID"])&&empty($_GET["PLAN_ID"])) $_GET["PLAN_ID"]=$_SESSION["PLAN_ID"];
		$model=D("Plan");
		$vo = $model->find("PLAN_ID=".$_GET["PLAN_ID"]);
		if($vo === false) 
		{ 
			$this->assign("jumpUrl",__URL__); 
			$this->error("不存在记录！"); 
		} else{
			$this->assign("vo",$vo);
			$_SESSION["PLAN_ID"]=$_GET["PLAN_ID"];
			//列出并统计数据
			$Model = new Model();
			$voList=$Model->query("SELECT OPTION_ID,PLAN_ID,d.OPTION_NAME,CASE WHEN NUM IS NULL THEN '0' ELSE NUM END AS NUM,CASE WHEN FINISH IS NULL THEN '0' ELSE FINISH END AS FINISH FROM (SELECT * FROM xtu_plan_option WHERE PLAN_ID = '".$_GET["PLAN_ID"]."') d  left join (SELECT a.OPTION_NAME,NUM,CASE WHEN FINISH IS NULL THEN '0' ELSE FINISH END AS FINISH FROM (SELECT OPTION_NAME,COUNT(*) as NUM  FROM `xtu_plan_quota` WHERE `PLAN_ID` = '".$_GET["PLAN_ID"]."' GROUP BY OPTION_NAME) a left join (SELECT OPTION_NAME,COUNT(*) as FINISH  FROM `xtu_plan_quota` WHERE `PLAN_ID` = '".$_GET["PLAN_ID"]."' AND `XH`!='' GROUP BY OPTION_NAME) b ON a.OPTION_NAME=b.OPTION_NAME) c on c.OPTION_NAME=d.OPTION_NAME ");		
			$this->assign("list",$voList);
			$this->display();
		}
	}
	function addPlanOption(){
		//项目子名称主页面
		$this->display();
	}
	function insertPlanOption(){
		//保存添加项目子名称
		$Dao = D("Plan_option"); 
		$vo = $Dao->create(); 
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		} 
 
		$rs = $Dao->add(); 
		if($rs){ 
			$this->assign("jumpUrl",__URL__); 
			$this->success("添加数据成功!"); 
		}else{ 
			$this->error("添加数据失败"); 
		} 		
	}	
    public function deletePlanOption()
    {
        //删除指定记录
        $model        = D('Plan_option');
        if(!empty($model)) {
            $id         = $_REQUEST['id'];
            if(isset($id)) {
				$list=$model->findAll("`OPTION_ID` IN (".$id.")");
				if($list)foreach($list as $tmp)
				{
					$Dao=D('Plan_quota');
					$Dao->delete("`PLAN_ID`='".$tmp['PLAN_ID']."' AND `OPTION_NAME`='".$tmp['OPTION_NAME']."'");
                }
				if($model->delete($id)){
					$this->assign("jumpUrl",__URL__."/optionIndex/PLAN_ID/".$_SESSION["PLAN_ID"]); 
                    $this->success(L('_DELETE_SUCCESS_'));
                }else {
                    $this->error(L('_DELETE_FAIL_'));
                }        	
            }else {
                $this->error('非法操作');
            }        	
        }
    }			
}
?>